package com.plugin.uniapp;

import android.content.Context;
import android.content.Intent;

import com.plugin.uniapp.utils.Logger;

import org.json.JSONObject;
import io.dcloud.feature.uniapp.annotation.UniJSMethod;
import io.dcloud.feature.uniapp.common.UniModule;

public class UniAppPlugin extends UniModule {
    @UniJSMethod(uiThread = true)
    public void openVideoActivity(String data) {
        Config.IsDebugMode = false;
        Config.TestDeviceId = "";
        Context context = mUniSDKInstance.getContext();
        try {
            if (data != null && !data.trim().isEmpty()) {
                JSONObject json = new JSONObject(data);
                if (json.has("api_base_url")) {
                    Config.APIBaseUrl = json.getString("api_base_url");
                }
                if (json.has("api_get_devices")) {
                    Config.APIGetDevices = json.getString("api_get_devices");
                }
                if (json.has("user_token")) {
                    Config.UserToken = json.getString("user_token");
                }
                if (json.has("is_login")) {
                    Config.IsLogin = json.getBoolean("is_login");
                }
                if (json.has("col")) {
                    Config.Col = json.getInt("col");
                }
                if (json.has("ws_base_url")) {
                    Config.WSBaseUrl = json.getString("ws_base_url");
                }
                if (json.has("api_timeout")) {
                    Config.APITimeout = json.getInt("api_timeout");
                }
                if (json.has("can_auto_popup")) {
                    Config.CanAutoPopup = json.getBoolean("can_auto_popup");
                }
                if (json.has("auto_popup_delay")) {
                    Config.AutoPopupDelay = json.getInt("auto_popup_delay");
                }
                if (json.has("task_start_suffix")) {
                    Config.TaskStartPrefix = json.getString("task_start_suffix");
                }
                if (json.has("task_doing_suffix")) {
                    Config.TaskDoingPrefix = json.getString("task_doing_suffix");
                }
                if (json.has("task_finish_suffix")) {
                    Config.TaskFinishSuffix = json.getString("task_finish_suffix");
                }
                if (json.has("task_not_login")) {
                    Config.TaskNotLogin = json.getString("task_not_login");
                }
                if (json.has("task_empty")) {
                    Config.TaskEmpty = json.getString("task_empty");
                }
                if (json.has("idle_task_id")) {
                    Config.IdleTaskId = json.getInt("idle_task_id");
                }
                if (json.has("test_device_id")) {
                    Config.TestDeviceId = json.getString("test_device_id");
                }
                if (json.has("is_debug_mode")) {
                    Config.IsDebugMode = json.getBoolean("is_debug_mode");
                }
            }
            Intent intent = new Intent(context, MainActivity.class);
            intent.putExtra("data", data);
            context.startActivity(intent);
        } catch (Exception e) {
            error(e.toString());
        }
    }

    private void log(String msg) {
        Logger.log(msg, getClass(), false);
    }

    private void error(String msg) {
        Logger.error(msg, getClass(), false);
    }
}
